<template>
  <div>
    <div class="query-form">
      <el-form ref="tableSearchParam" :model="tableSearchParam" label-width="50px" :inline="true">
        <el-form-item :label=item.name :label-width="item.labelWidth" v-for="item in searchColumns">
          <el-input v-model="item.value" style="width:200px" size="small" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchTableData" icon="el-icon-search" size="small">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <br/>
    <el-table
      :data="tableData"
      border
      empty-text="暂无数据"
      style="width: 100%"
      @row-click="tableRowClick"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        type="index"
        width="35">
      </el-table-column>
      <el-table-column
        v-for="item in tableColumns"
        :label=item.label
        :property=item.name
        :width=item.width>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page=this.currentPage
        :page-sizes="[10, 20, 30, 40]"
        :page-size=this.pageSize
        layout="total, sizes, prev, pager, next, jumper"
        :total=this.total>
      </el-pagination>
    </div>
  </div>
</template>
<style scoped>
  .query-form {
    border-bottom: 1px solid #ebeef5;
    padding-top: 0px;
  }
</style>

<script>

  export default {
    props: {
      listMethod: Function,
      searchColumns: Array,
      tableColumns: Array
    },
    data() {
      return {
        tableData: [],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        tableSearchParam: {},
        multipleSelection: []
      }
    },

    created() {
      this.listTableData()
    },
    methods: {
      listTableData() {
        this.listMethod(this.tableSearchParam, this.currentPage, this.pageSize, (res) => {
          this.tableData = res.list
          this.total = res.total
        }, () => {
        })
      },
      searchTableData() {
        for (const sc of this.searchColumns) {
          this.$set(this.tableSearchParam, sc.code, sc.value)
        }
        this.listTableData()
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.listTableData()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.listTableData()
      },
      tableRowClick(row, event, column) {
        this.$emit('row', row);
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    }
  }
</script>
